//var $WEB_ROOT_PATH = getContextPath();
function getContextPath() {
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    var basePath = prePath;
    basePath = prePath + postPath;
    return basePath;
}

var $WEB_ROOT_PATH = getContext();

function getContext() {
    var pathName = window.location.pathname.substring(1);
    var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
    // if (webName == "") {
    // 	return window.location.protocol + '//' + window.location.host;
    // } else {
    // 	return window.location.protocol + '//' + window.location.host + '/'+ webName;
    // }
    return pathName.substring(0, 0); //linux server
}

// sessionStorage current page
var $TokenUtil = {
    token: 'TokenAz',
    sub: 'sub',
    perm: 'perm',
    setToken: function (value) {
        sessionStorage.setItem($TokenUtil.token, value);
    },
    setSub: function (value) {
        sessionStorage.setItem($TokenUtil.sub, value);
    },
    setPerm: function (value) {
        sessionStorage.setItem($TokenUtil.perm, value);
    },
    getToken: function () {
        return sessionStorage.getItem($TokenUtil.token);
    },
    getSub: function () {
        return sessionStorage.getItem($TokenUtil.sub);
    },
    getPerm: function () {
        return sessionStorage.getItem($TokenUtil.perm);
    },
    clearToken: function () {
        sessionStorage.removeItem($TokenUtil.token);
    },
    clearPerm: function () {
        sessionStorage.removeItem($TokenUtil.perm);
    },
    clearSub: function () {
        sessionStorage.removeItem($TokenUtil.sub);
    }
}

var $TokenHeaders = {
    'TokenAz': $TokenUtil.getToken()
}

/**
 * 重新登录
 * @type {{reLogin: $TokenReUtil.reLogin}}
 */
var $TokenReUtil = {
    reLogin: function () {
        $.messager.alert("提示", "登录过期，请重新登录", "", function () {
            // 跳出iframe
            if (top.location !== self.location) {
                top.location = $WEB_ROOT_PATH + "/";
            } else {
                window.location.href = $WEB_ROOT_PATH + "/";
            }
        });
    }
}

var $WEB_ROOT_IP = getIpPath();

function getIpPath() {
    var hostport = document.location.host;
    var arr = hostport.split(':');
    var ip = arr[0];
    return ip;

}

var $WEB_ROOT_PORT = getPortPath();

function getPortPath() {
    var hostport = document.location.host;
    var arr = hostport.split(':');
    var port = arr[1];
    return port;
}


$.ajaxSetup({
    cache: false,
    complete: function (XMLHttpRequest, textStatus) {
        var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
        if (sessionstatus == "timeout") {
            if (window.top != window.self) {
                window.top.location = "${pageContext.request.contextPath}";
            }
            else {
                window.location.replace("${pageContext.request.contextPath}");
            }
        }
    }
});

(function ($) {
    $.fn.serializeJson = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        //var str=this.serialize();
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    };
})(jQuery);

(function ($) {
    $.fn.serializeJsonStr = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        //var str=this.serialize();
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return JSON.stringify(serializeObj);
    };
})(jQuery);

(function ($) {
    $.postJson = function (url, data, callback, type) {
        // shift arguments if data argument was omitted
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
})(jQuery);

(function ($) {
    $.postJsonAsync = function (url, data, callback, type) {
        // shift arguments if data argument was omitted
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            type: 'POST',
            async: false,
            contentType: 'application/json',
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
})(jQuery);

// 异步-ajaxAsync-tokenAz
(function ($) {
    $.ajaxAsync = function (url, requestType, data, successBack, errorBack, dataType) {
        if (jQuery.isFunction(data)) {
            dataType = dataType || successBack;
            successBack = data;
            data = undefined;
        }
        return jQuery.ajax({
            headers: $TokenHeaders,
            type: requestType,
            async: true, //默认异步
            url: url,
            data: data,
            success: successBack,
            error: errorBack,
            dataType: dataType
        });
    }
})(jQuery);

// 同步-ajaxSync-tokenAz-param
(function ($) {
    $.ajaxSync = function (url, requestType, data, successBack, errorBack, dataType) {
        if (jQuery.isFunction(data)) {
            dataType = dataType || successBack;
            successBack = data;
            data = undefined;
        }
        return jQuery.ajax({
            headers: $TokenHeaders,
            type: requestType,
            async: false, //同步false
            url: url,
            data: data,
            success: successBack,
            error: errorBack,
            dataType: dataType
        });
    }
})(jQuery);

// 异步-ajaxAsyncJson-tokenAz-param
(function ($) {
    $.ajaxAsyncJson = function (url, requestType, data, successBack, errorBack, dataType) {
        if (jQuery.isFunction(data)) {
            dataType = dataType || successBack;
            successBack = data;
            data = undefined;
        }
        return jQuery.ajax({
            headers: $TokenHeaders,
            type: requestType,
            async: true, //默认异步
            contentType: 'application/json',
            url: url,
            data: data,
            success: successBack,
            error: errorBack,
            dataType: dataType
        });
    }
})(jQuery);

function initPagination(p) {
    $(p).pagination({
        pageSize: 20,//每页显示的记录条数，默认为20
        pageList: [20, 50, 100, 200, 500, 1000],//可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',

        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }

    });
}

/**
 * js屏蔽backspace键
 */
$(document).keydown(function (e) {
    e = window.event || e;
    var code = e.keyCode || e.which;
    if (code == 8) {
        var src = e.srcElement || e.target;
        var tag = src.tagName;
        if (tag != "INPUT" && tag != "TEXTAREA") {
            e.returnValue = false;
            return false;
        } else if ((tag == "INPUT" || tag == "TEXTAREA") && src.readOnly == true) {
            e.returnValue = false;
            return false;
        }
    }
});
